﻿using System;
using System.Linq;
using Domain.Entity;

namespace QQZoneMarketingDao
{
    public class QqEmailDao
    {
        /// <summary>
        /// 判断该Email是否已经被保存过了
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool EmailHasVisited(string email)
        {
            using (var db = new DBDataContext())
            {
                if (db.QQEmail.Any(x => x.Email == email))
                    return true;
                return false;
            }
        }
        public static QQEmail GetLastQQUserEmail()
        {
            using (var db = new DBDataContext())
            {
                var last = db.QQEmail.Max(x => x.CreateOn);
                return db.QQEmail.FirstOrDefault(x => x.CreateOn == last);
            }
        }
        /// <summary>
        /// 获取未曾访问过的QQ空间
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string[] GetHasNoVisiteQQZoneUserEmail(int count)
        {
            using (var db = new DBDataContext())
            {
                var emails = db.QQEmail.Where(x => x.HasQQZoneVisited == false).Take(count).Select(x => x.Email);
                return emails.ToArray();
            }
        }

        /// <summary>
        /// 设置QQ空间已经访问过了
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static void SetQQZonHasVisited(string email)
        {
            using (var db = new DBDataContext())
            {
                var qqemail = db.QQEmail.FirstOrDefault(x => x.Email == email);
                if (qqemail != null)
                {
                    qqemail.HasQQZoneVisited = true;
                }
                db.SubmitChanges();
            }
        }
    }
}
